//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:"order.list/",
		account:app.setting.account,
		loading:true,
		pagetitle:'订单列表' + '-' + app.setting.title,
		
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			console.log('orderlist');console.log(pagedata)
			page.setData({
				pluginnav:app.session.menus,
				pagedata:pagedata,
				action:app.access.plugin+'/'+app.access.action+'/',
			});
			if(!app.isShowSubMsg && pagedata.tmplIds && pagedata.tmplIds[0]){
				app.util.getsubmsgSet(pagedata.tmplIds,function(ShowSubMsg){
					if(ShowSubMsg){
						app.isShowSubMsg=true;
						page.setData({
							ShowSubMsg:true,
						});
					}
				});
			}
			setTimeout(()=>{page.getlist(1);},500);
		});
	},
	//订阅消息
	SubscribeMessage: function () {
		var the=this
		var pagedata=the.data.pagedata
		the.setData({
				ShowSubMsg:false,
		});
		wx.requestSubscribeMessage({
			'tmplIds':pagedata.tmplIds,
			success:function (res) {
				console.log('SubscribeMessage success');console.log(res);
				if(res[pagedata.tmplIds[0]]=='accept'){
					
				}
			},
			fail:function (res) {
				console.log('SubscribeMessage fail');console.log(res);
			},
		})
	},
	getlist:function(e){
		var the=this
		var page=the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		var data={}
		the.setData({
			loading:true,
		});
		if(e){
			if(e.currentTarget){
				data = e.currentTarget.dataset;
				pagedata.status=data.status
			}
			pagedata.list=[]
			page=1
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
					loading:false,
				});
				return false;
			}
		}
		var userdeleted=data.userdeleted?data.userdeleted:'';
		app.util.get('jy_weishop/order.list/getlist/status='+pagedata.status+'&userdeleted='+userdeleted+'&page='+page,function(res){
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.list':pagedata.list,
					'pagedata.status':pagedata.status,
					'pagedata.userdeleted':userdeleted,
					loading:false,
				});
			}else{
				console.log(page);
				pagedata.list = pagedata.list.concat(res.list)
				page++
				the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.status':pagedata.status,
					'pagedata.userdeleted':userdeleted,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
		});
	},
	//翻页
	onReachBottom: function () {
		var the=this;
		var isnodata=the.data.isnodata
		var loading=the.data.loading
		if(!isnodata && !loading){
			the.getlist()
		}
	},
	cancel: function (e) {
		var the=this
		var pagedata=the.data.pagedata
		var id = e.currentTarget.dataset.id;
		var index = e.currentTarget.dataset.index;
		  app.util.post('jy_weishop/order.list/cancel/', { 'id': id },function(res){
			  app.util.msg(res);
			  the.getlist(1);
		  });		
	},
	receive: function (e) {
		var the=this
		var pagedata=the.data.pagedata
		var id = e.currentTarget.dataset.id;
		app.util.post('jy_weishop/order.list/receive/',{'id':id},function(res){
		  app.util.msg(res);
		  the.getlist(1);
	  });
	},
	closepaymodal:function(){
		var page=this
		page.setData({
				showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		app.util.pay_credit(data.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay_wechat:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		app.util.pay_wechat(data.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay_alipay:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		app.util.pay_alipay(data.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay_hdfk:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		app.util.pay_hdfk(data.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay: function (e) {
		var page=this
		var data = e.currentTarget.dataset;
		app.util.post('jy_weishop/order.list/pay/id='+data.id,'',function(payid){
			console.log('payid');console.log(payid);
			app.util.pay(payid,function(res){		
				if(res.type=='more'){
					page.setData({
						showpaymodal:true,
						'pagedata.paymodal':res
					});
				}else if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message);
				}
			});
		});
	},
  	click:function(evt){
		var the=this;
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	}
});